package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceService;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeviceServiceMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.DSERVICE_ID dserviceId,  \n" +
            " temp.DSERVICE_CODE dserviceCode,  \n" +
            " temp.DSERVICE_NAME dserviceName,\n" +
            " temp.DSERVICE_ARG dserviceArg\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_DEVICE_SERVICE t\n" +
            "\tWHERE (t.DSERVICE_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.DSERVICE_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')) \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.DSERVICE_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<DeviceService> findDeviceServiceList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.DSERVICE_ID) FROM T_DEVICE_SERVICE a WHERE a.DSERVICE_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.DSERVICE_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countDeviceServiceList(String query);

    @Insert("<script>" +
            "INSERT INTO T_DEVICE_SERVICE \n" +
            "(DSERVICE_ID,DSERVICE_CODE,DSERVICE_NAME" +
            "<if test=\" dserviceArg != null\">"+
            ",DSERVICE_ARG" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{dserviceId},#{dserviceCode},#{dserviceName}" +
            "<if test=\" dserviceArg != null\">"+
            ",#{dserviceArg}" +
            "</if>"+
            ")" +
            "</script>")
    void addDeviceServiceInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.DSERVICE_ID dserviceId,  \n" +
            " temp.DSERVICE_CODE dserviceCode,  \n" +
            " temp.DSERVICE_NAME dserviceName,\n" +
            " temp.DSERVICE_ARG dserviceArg\n" +
            "FROM T_DEVICE_SERVICE temp\n" +
            "\tWHERE temp.DSERVICE_ID = #{params} OR temp.DSERVICE_CODE = #{params}\n" +
            "</script>")
    DeviceService findDeviceServiceInfoByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_DEVICE_SERVICE SET \n" +
            "\tDSERVICE_CODE = #{dserviceCode},\n" +
            "\tDSERVICE_NAME = #{dserviceName}\n" +
            "<if test=\" dserviceArg != null\">"+
            "\t,DSERVICE_ARG = #{dserviceArg}\n" +
            "</if>"+
            "WHERE DSERVICE_ID = #{dserviceId}" +
            "</script>")
    void updateDeviceServiceInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_DEVICE_SERVICE WHERE DSERVICE_ID = #{dserviceId}</script>")
    void deleteDeviceServiceInfo(String dserviceId);

    @Select("<script>SELECT count(MODEL_ID) FROM T_DEVICE_MODEL WHERE MODEL_ARG LIKE CONCAT(CONCAT('%', CONCAT(CONCAT('\"', #{query}),'\"')),'%')</script>")
    int countServiceUseTotal(String dserviceCode);

    @Update("<script>update T_DEVICE_MODEL set MODEL_ARG = replace(MODEL_ARG,CONCAT(CONCAT('\"', #{oldDserviceCode}),'\"'),CONCAT(CONCAT('\"', #{dserviceCode}),'\"'))</script>")
    void updateDeviceModelArg(JSONObject params);
}
